<template>
  <div class="app-container">
    <div class="card-intro">
      <p>规则介绍：十连模拟必出四星以上物品，累计八十次必出五星以上物品。</p>
      <p>六星概率为0.1%，五星概率为0.9%。您可在旁边的菜单栏查看排行榜，模拟统计以及背包。</p>
      <p class="card-count">
        <span class="highlight-you">您</span>
        <span class="highlight-current">目前</span> 
        还有<span class="highlight-num">{{ cardNum }}</span>次模拟次数，
        距离出四星物品还差<span class="highlight-count">{{ 10 - smallGuarantee }}</span>次，
        五星物品还差<span class="highlight-count">{{ 80 - bigGuarantee }}</span>次。
      </p>
    </div>

    <div class="card-actions">
      <el-button 
        type="primary" 
        icon="el-icon-document" 
        round 
        @click="drawCard(1, 0)"
        class="draw-btn single-draw"
      >
        单次模拟
      </el-button>
      <el-button 
        type="primary" 
        plain 
        icon="el-icon-document" 
        @click="drawCard(10, 0)"
        class="draw-btn ten-draw"
      >
        十连模拟
      </el-button>
    </div>

    <div v-if="showCardList" class="card-results">
      <div class="result-items">
        <span 
          v-for="(item, index) in cardList" 
          :key="index"
          :class="getCardClass(item.level)"
        >
          【{{ item.cardName }}】
        </span>
      </div>
      <div v-if="showCardList && cardList.length > 0" class="luck-score">
        <span>本次十连模拟欧气值为：</span>
        <span class="score-value">{{ score }}</span>
      </div>
    </div>

    <h3 class="pool-title">本期奖池</h3>
    <el-table 
      ref="tables" 
      v-loading="loading"
      :data="list"
      class="card-pool-table"
      :header-cell-style="{background: '#f5f7fa', color: '#606266'}"
    >
      <el-table-column type="index" width="50" align="center" />
      <el-table-column label="卡片名称" align="center" width="110" prop="cardName" />
      <el-table-column label="卡片星级" align="center" prop="level">
        <template slot-scope="scope">
          <el-tag :type="getStarTagType(scope.row.level)" effect="dark">
            {{ scope.row.level }}星
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="卡片欧气值" align="center" prop="score" />
    </el-table>

    <pagination 
      v-show="total > 0" 
      :total="total" 
      :page.sync="queryParams.pageNum" 
      :limit.sync="queryParams.pageSize"
      @pagination="selectRecord"
    />
  </div>
</template>

<script>
import { getCardList, drawCard, userInfo } from "@/api/card/draw";

export default {
  name: "card",
  data() {
    return {
      loading: true,
      list: [],
      rankList: [],
      cardNum: undefined,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined,
        num: undefined,
        mode: undefined
      },
      total: 0,
      showCardList: false,
      cardList: [],
      form: {},
      smallGuarantee: 0,
      bigGuarantee: 0,
      score: 0
    };
  },
  created() {
    this.selectRecord();
    this.getCardUserInfo();
  },
  methods: {
    getStarTagType(level) {
      const types = {
        3: 'info',
        4: 'primary',
        5: 'warning',
        6: 'danger'
      };
      return types[level] || '';
    },
    getCardClass(level) {
      return {
        'three-star': level === 3,
        'four-star': level === 4,
        'five-star': level === 5,
        'six-star': level === 6,
        'card-item': true
      }
    },
    selectRecord() {
      this.loading = true;
      getCardList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.loading = false;
        this.list = response.rows;
        this.total = response.total;
      });
    },
    getCardUserInfo() {
      userInfo().then(response => {
        this.cardNum = response.data.drawTicket;
        this.smallGuarantee = response.data.smallGuarantee;
        this.bigGuarantee = response.data.bigGuarantee;
      });
    },
    drawCard(num, mode) {
      this.queryParams.num = num;
      this.queryParams.mode = mode;
      if (num > this.cardNum) {
        this.showCardList = true;
        this.cardList = [{ cardName: "抽卡次数不足", level: 0 }];
        return;
      }
      drawCard(this.queryParams).then(response => {
        this.showCardList = true;
        this.cardNum = response.data.userInfo.drawTicket;
        this.cardList = response.data.record;
        this.smallGuarantee = response.data.userInfo.smallGuarantee;
        this.bigGuarantee = response.data.userInfo.bigGuarantee;
        this.score = response.data.score;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 25px;
  max-width: 1200px;
  margin: 0 auto;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e8ed 100%);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.card-intro {
  background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
  border-radius: 12px;
  padding: 25px;
  margin-bottom: 25px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  border-left: 5px solid #409eff;
  
  p {
    margin: 12px 0;
    color: #333;
    line-height: 1.7;
    font-size: 15px;
    
    &.card-count {
      font-size: 17px;
      font-weight: 600;
      padding: 10px 15px;
      background: rgba(64, 158, 255, 0.05);
      border-radius: 8px;
    }
  }
}

.highlight-you {
  color: #f56c6c;
  font-weight: bold;
  font-size: 18px;
}

.highlight-current {
  font-size: 17px;
  color: #666;
  font-weight: 500;
}

.highlight-num {
  color: #409eff;
  font-weight: bold;
  font-size: 20px;
  margin: 0 3px;
}

.highlight-count {
  color: #e6a23c;
  font-weight: bold;
  font-size: 18px;
  margin: 0 3px;
}

.card-actions {
  margin: 30px 0;
  text-align: center;
  display: flex;
  justify-content: center;
  gap: 20px;
  
  .draw-btn {
    margin: 0;
    padding: 15px 30px;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 1px;
    border: none;
    
    &.single-draw {
      background: linear-gradient(135deg, #409eff 0%, #337ecc 100%);
      box-shadow: 0 4px 15px rgba(64, 158, 255, 0.3);
    }
    
    &.ten-draw {
      background: linear-gradient(135deg, #67c23a 0%, #529b2e 100%);
      box-shadow: 0 4px 15px rgba(103, 194, 58, 0.3);
      color: white;
    }
    
    &:active {
      transform: translateY(1px);
    }
  }
}

.card-results {
  background: white;
  border-radius: 12px;
  padding: 25px;
  margin-bottom: 25px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
  
  .result-items {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 15px;
    justify-content: center;
    
    @media (max-width: 768px) {
      gap: 4px;
      justify-content: space-between;
    }
  }
  
  .card-item {
    font-weight: bold;
    padding: 6px 10px;
    border-radius: 4px;
    display: inline-block;
    font-size: 14px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
    white-space: nowrap;
    
    @media (max-width: 768px) {
      padding: 3px 5px;
      font-size: 11px;
      flex: 0 0 calc(20% - 4px);
      text-align: center;
      box-sizing: border-box;
      max-width: calc(20% - 4px);
      overflow: hidden;
      text-overflow: ellipsis;
      margin-bottom: 4px;
    }
  }
  
  .three-star {
    color: #00b4d8;
    background: linear-gradient(135deg, rgba(0, 180, 216, 0.15) 0%, rgba(0, 180, 216, 0.25) 100%);
    border: 1px solid rgba(0, 180, 216, 0.3);
  }
  
  .four-star {
    color: #9c27b0;
    background: linear-gradient(135deg, rgba(156, 39, 176, 0.15) 0%, rgba(156, 39, 176, 0.25) 100%);
    border: 1px solid rgba(156, 39, 176, 0.3);
  }
  
  .five-star {
    color: #ff9800;
    background: linear-gradient(135deg, rgba(255, 152, 0, 0.15) 0%, rgba(255, 152, 0, 0.25) 100%);
    border: 1px solid rgba(255, 152, 0, 0.3);
  }
  
  .six-star {
    color: #f44336;
    background: linear-gradient(135deg, rgba(244, 67, 54, 0.15) 0%, rgba(244, 67, 54, 0.25) 100%);
    border: 1px solid rgba(244, 67, 54, 0.3);
    animation: pulse 1.5s infinite;
    position: relative;
    overflow: hidden;
    
    &::after {
      content: '';
      position: absolute;
      top: -50%;
      left: -50%;
      width: 200%;
      height: 200%;
      background: linear-gradient(
        to bottom right,
        rgba(255, 255, 255, 0) 45%,
        rgba(255, 255, 255, 0.8) 50%,
        rgba(255, 255, 255, 0) 55%
      );
      transform: rotate(30deg);
      animation: shine 3s infinite;
    }
  }
  
  .luck-score {
    text-align: center;
    font-size: 17px;
    font-weight: 500;
    padding: 12px;
    background: rgba(245, 108, 108, 0.05);
    border-radius: 8px;
    margin-top: 15px;
    
    .score-value {
      color: #f56c6c;
      font-weight: bold;
      font-size: 20px;
      margin-left: 8px;
    }
  }
}

.pool-title {
  color: #333;
  margin: 30px 0 20px;
  font-size: 20px;
  font-weight: 600;
  padding-left: 15px;
  border-left: 5px solid #409eff;
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    left: 0;
    bottom: -5px;
    width: 100px;
    height: 2px;
    background: linear-gradient(90deg, #409eff, transparent);
  }
}

.card-pool-table {
  margin-top: 15px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
  
  ::v-deep .el-table__row {
    transition: background-color 0.2s;
    
    &:hover {
      background-color: #f5f7fa !important;
    }
  }
  
  ::v-deep .el-table__header-wrapper {
    border-radius: 8px 8px 0 0;
  }
}

@keyframes pulse {
  0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(244, 67, 54, 0.4); }
  70% { transform: scale(1.05); box-shadow: 0 0 0 10px rgba(244, 67, 54, 0); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(244, 67, 54, 0); }
}

@keyframes shine {
  0% { transform: translateX(-100%) rotate(30deg); }
  100% { transform: translateX(100%) rotate(30deg); }
}

@media (max-width: 768px) {
  .app-container {
    padding: 15px;
  }
  
  .card-actions {
    flex-direction: column;
    gap: 15px;
    
    .draw-btn {
      width: 100%;
      padding: 12px;
    }
  }
  
  .card-results {
    padding: 12px;
    
    .result-items {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      gap: 2px;
      margin-bottom: 10px;
    }
    
    .card-item {
      width: calc(20% - 2px);
      margin-bottom: 2px;
      font-size: 9px;
      padding: 1px 2px;
      line-height: 1.2;
      border-radius: 2px;
    }
  }
  
  .card-intro {
    padding: 12px;
    
    p {
      font-size: 13px;
      margin: 8px 0;
      
      &.card-count {
        font-size: 14px;
        padding: 8px 10px;
      }
    }
  }

  .luck-score {
    font-size: 14px !important;
    padding: 8px !important;
    
    .score-value {
      font-size: 16px !important;
    }
  }
}
</style>